package com.ruoyi.wisdom.event;

import com.ruoyi.wisdom.domain.IotSchoolSetting;
import com.ruoyi.wisdom.domain.swap.SwapSchoolSetting;
import com.ruoyi.wisdom.service.IIotSchoolSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SchoolSettingEvent extends IotEvent{

    @Autowired
    private IIotSchoolSettingService iotSchoolSettingService;

    /**
     * 处理
     * @param swapSchoolSetting
     */
    public void handle(SwapSchoolSetting swapSchoolSetting) {
        if (swapSchoolSetting.getOperate() == null) {
            save(swapSchoolSetting);
        } else {
            switch (swapSchoolSetting.getOperate()) {
                case "delete":
                case "remove":
                    delete(swapSchoolSetting);
                    break;
                default:
                    save(swapSchoolSetting);
            }
        }
    }

    /**
     * 添加|编辑
     * @param swapSchoolSetting
     */
    public void save(SwapSchoolSetting swapSchoolSetting)
    {
        IotSchoolSetting result = iotSchoolSettingService.selectIotSchoolSettingByName(swapSchoolSetting.getName());
        // 实体装换
        IotSchoolSetting iotSchoolSetting = getModelMapper().map(swapSchoolSetting, IotSchoolSetting.class);
        if (result != null) {
            iotSchoolSetting.setId(result.getId());
            iotSchoolSettingService.updateIotSchoolSetting(iotSchoolSetting);
        } else {
            iotSchoolSettingService.insertIotSchoolSetting(iotSchoolSetting);
        }
    }

    /**
     * 删除
     * @param swapSchoolSetting
     */
    public void delete(SwapSchoolSetting swapSchoolSetting){
        iotSchoolSettingService.deleteIotSchoolSettingByName(swapSchoolSetting.getName());
    }
}
